package droid.training;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

public class Measure extends Activity{
	public DataHelper dh;
	private Button mSave;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.measure);
       /* TextView textview = new TextView(this);
        textview.setText("This is the Measure tab");
        setContentView(textview);*/
        this.dh = new DataHelper(this);
        mSave = (Button)findViewById(R.id.mSave);
        mSave.setOnClickListener(mSaveListener);
        
    }

private OnClickListener mSaveListener = new OnClickListener() {
    public void onClick(View v) {
    	String values;
    	/*EditText mText = (EditText) findViewById(R.id.numpicker_tomeg);
    	mText.getText().toString();*/
    	DatePicker dp=(DatePicker)findViewById(R.id.mdatePicker);
    	String date = dp.getYear() +"-"+formatDate(dp.getMonth()+1)+"-"+formatDate(dp.getDayOfMonth());
    	NumberPicker tomegp=(NumberPicker)findViewById(R.id.numpicker_tomeg);
    	int tomeg = tomegp.getCurrent();
    	NumberPicker csipop=(NumberPicker)findViewById(R.id.numpicker_csipo);
    	int csipo = csipop.getCurrent();
    	NumberPicker derekp=(NumberPicker)findViewById(R.id.numpicker_derek);
    	int derek = derekp.getCurrent();
    	NumberPicker balp=(NumberPicker)findViewById(R.id.numpicker_blab);
    	int bal = balp.getCurrent();
    	NumberPicker jobbp=(NumberPicker)findViewById(R.id.numpicker_jlab);
    	int jobb = jobbp.getCurrent();
    	values=date+"; "+tomeg+"; "+derek+"; "+csipo+"; "+bal+"; "+jobb+"\n";
    	Log.d("TEXTVALUE", values);
    	
    	
    	Toast msg;
    	String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    	/* FileWriter fstream = new FileWriter("out.txt");
         BufferedWriter out = new BufferedWriter(fstream);
         out.write("Hello Java");
         //Close the output stream
         out.close();*/
    	
    	 /*	OutputStream outStream = null;
    	DataOutputStream dos;*/
    	//File file = new File(extStorageDirectory, "data.txt");
    	FileOutputStream fOut;
        OutputStreamWriter osw;  

    	try {
    		fOut = openFileOutput("measure_data.txt", MODE_APPEND);
    		osw = new OutputStreamWriter(fOut);
    		//dos=new DataOutputStream(outStream);
    		osw.write(values);
    	    osw.flush();
    	    osw.close();
    		msg = Toast.makeText(getApplicationContext(), "Saved to File", Toast.LENGTH_SHORT); 	  
    	}
    	catch (FileNotFoundException e) {
    	    // TODO Auto-generated catch block
    	    e.printStackTrace();
    	    msg = Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT);
    	} 
    	catch (IOException e) {
    	    // TODO Auto-generated catch block
    	    e.printStackTrace();
    	    msg = Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT);
    	}
    	catch(Exception e) {
    	    // TODO Auto-generated catch block
    	    e.printStackTrace();
    	    msg = Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT);
    	}
    	
    	//write to DB
    	try{
            dh.insert_measure(date, tomeg, derek, csipo, bal, jobb);
            msg = Toast.makeText(getApplicationContext(), "Saved to DB", Toast.LENGTH_SHORT);
        }
    	catch(Exception e) {
    	    // TODO Auto-generated catch block
    	    e.printStackTrace();
    	    msg = Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG);
    	}
    	msg.show();

    }
};

	public String formatDate(int dc){
		if(dc<10)
			return "0"+dc;
		else
			return ""+dc;
}
}
